package com.webserver.core;

import com.webserver.http.EmptyRequestException;
import com.webserver.http.HttpServeletRequest;
import com.webserver.http.HttpServeletResponse;

import javax.activation.MimetypesFileTypeMap;
import java.io.IOException;
import java.net.Socket;

public class ClientHandler implements Runnable{
    private Socket socket;
    public ClientHandler(Socket socket) {
        this.socket = socket;
    }
    public void run() {
        try {
            //解析请求行
            HttpServeletRequest request = new HttpServeletRequest(socket);
            HttpServeletResponse response = new HttpServeletResponse(socket);
            //处理请求行
            DispatcherServlet servlet = DispatcherServlet.getInstance();
            servlet.service(request,response);
            //发送响应
            response.response();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (EmptyRequestException e) {
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
